class CoresController < ApplicationController
  def home
    
  end
  
  def view
    unless @core = Core.find(:first, :conditions => ["alias = ?", params[:id]])
      flash[:error] = "core_not_found"
      redirect_to "/"
    end    
    @comments_count = Comment.count(:conditions => ["core_id = ? ", @core.id]) 
    @comments = Comment.find(:all,:include => :user, 
      :conditions => ["core_id = ? ", @core.id], 
      :offset => @comments_count > 20 ? @comments_count - 20 : 0, 
      :limit => 20, :order => "id" )
    @comment = Comment.new
    @multimedia_count = Multimedia.count_by_sql(["SELECT COUNT(*) FROM cores_multimedias WHERE core_id = ?", @core.id])
    @multimedias = Multimedia.find(:all,:include => :user,
      :joins => " INNER JOIN cores_multimedias ON multimedias.id = cores_multimedias.multimedia_id AND cores_multimedias.core_id = #{@core.id}", 
      :offset => @multimedia_count > 20 ? @multimedia_count - 20 : 0, 
      :limit => 20, :order => "id" )    
  end
  
  def set_vote
    if (not params[:core_id].blank? and not params[:vote].blank? and current_user and 
          (core = Core.find(:first,:conditions => ["id = ?", params[:core_id]] )))
        vote_new = params[:vote].to_s == "yes" ? 1 : -1
       if(vote_old = Vote.find(:first, :conditions => ["core_id = ? and user_id = ?",params[:core_id], current_user.id] ))
          core.vote -= vote_old.vote
          Vote.delete_all(:core_id => params[:core_id].to_i, :user_id =>  current_user.id)          
       end
        Vote.create(:core_id => params[:core_id].to_i, :user_id =>  current_user.id, 
          :vote => vote_new)        
        core.vote += vote_new
        core.save
        
        render :json => {:user_id => current_user.id, :vote => core.vote, :user_color => current_user.colors, 
        :user_name => current_user.name, :user_alias => current_user.alias}
    end
  end
  
  def set_bookmark
     b = 0
    if not params[:core_id].blank? and current_user and 
      core = Core.find(:first, :conditions => { :id => params[:core_id] })          
         if current_user.bookmarks.exists?(core)
           current_user.bookmarks.delete(core)
         else
           current_user.bookmarks << core
           b = 1
        end    
        render :json => { :insert => b }
    end
  end
  
  def new_comment
    if params[:comment] and params[:comment][:message].to_s != ""
      c = Comment.new(:user_id => current_user.id)
      c.attributes = params[:comment]
      c.save
      get_comments
    end
  end
  

  
  def get_comments
    comments = [];    
    page = params[:page] 
    core = params[:core_id] || params[:comment][:core_id]
    if(last = params[:last_comment_id])
       c = Comment.find(:first, :conditions => ["id = ?", last])
      @last_user = c.user_id if c
    end
    comments_count = Comment.count(:conditions => ["core_id = ? ", core])    
    offset = page ? (comments_count / 20 - page.to_i) * 20  : 0 
    comments = if (!last.blank?)
      Comment.find(:all, :include => :user, :conditions => ["core_id = ? and id > ?", 
          core, last], :order => "id desc", 
          :limit => 20)
    else      
      Comment.find(:all, :include => :user, :conditions => ["core_id = ?", 
                       core], :order => "id desc", :offset => offset, :limit => 20 )
    end
    render :partial => "comments", :object => comments.reverse    
  end
  
  def google_images
    params[:start] = params[:start].to_i * 8   
    @google_images = GoogleImage.all(params[:keywords], params[:start])
    render :inline => "<% for image in @google_images %><%= link_to(image_tag(image.thumbnail), '#', :class => 'google_image', :onclick=>\"return importGoogleImage('#{ params[:name] }','\" + image.original + \"','\" + image.thumbnail + \"')\") %><% end %>" +
      "<div class='separator'><span></span></div>"
  end

end
